﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CNVP.Framework.Utils;

namespace CNVP.JShop.Web.Admin.Template
{
    public partial class FileManger : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!BLL.BLLCheckAdmin.LoginOk(Page, 54)) return;//判断页面权限
            var path = Request.QueryString["path"];
            var action = Request.QueryString["action"];
            var fileName = String.Empty;
            if (path == null)
            {
                hfCurentPath.Value = @"~/Template/Default/";
            }
            else
            {
                hfCurentPath.Value = path.ToString();
            }
            if (action == null)
            {
                Response.Redirect("Template.aspx?path=" + hfCurentPath.Value, true);
            }
            else
            {
                hfAction.Value = action.ToString();
            }
            if (!IsPostBack)
            {
                switch (action)
                {
                    case "AddFile":
                        lbTitle.Text = " > 添加文件";
                        break;
                    case "AddFolder":
                        lbTitle.Text = " > 添加文件夹";
                        FileContent.Visible = false;
                        break;
                    case "EditFolder":
                        lbTitle.Text = " > 重命名文件夹";
                        FileContent.Visible = false;
                        fileName = Request.QueryString["file"];
                        if (fileName == null)
                        {
                            Response.Redirect("Template.aspx?path=" + hfCurentPath.Value, true);
                        }
                        txtFileName.Text = fileName;
                        hfCurentFile.Value = fileName;
                        break;
                    case "EditFile":
                        lbTitle.Text = " > 修改文件";
                        fileName = Request.QueryString["file"];
                        if (fileName == null)
                        {
                            Response.Redirect("Template.aspx?path=" + hfCurentPath.Value, true);
                        }
                        if (FileUtils.FileExists(Server.MapPath(hfCurentPath.Value + fileName)))
                        {
                            txtFileName.Text = fileName;
                            txtFileContent.Text = FileUtils.ReadFile(Server.MapPath(hfCurentPath.Value + fileName));
                        }
                        hfCurentFile.Value = fileName;
                        break;
                    default:
                        break;
                }
            }
        }

        protected void btUp_Click(object sender, EventArgs e)
        {
            if (txtFileName.Text == "")
            {
                MessageUtils.ShowPre("名称不能为空！");
                return;
            }
            BLL.CNVP_UnitsInfo bll = new BLL.CNVP_UnitsInfo();
            if (hfAction.Value == "AddFile" && !bll.IsAllowFile(txtFileName.Text.Substring(txtFileName.Text.LastIndexOf(".")+1)))
            {
                MessageUtils.ShowPre("未被接受的文件类型。请更换文件名后缀！" + txtFileName.Text.Substring(txtFileName.Text.LastIndexOf(".") + 1));
                return;
            }
            if ((hfAction.Value == "AddFile" || hfAction.Value == "EditFile") && txtFileContent.Text == "")
            {
                MessageUtils.ShowPre("内容不能为空！");
                return;
            }
            switch (hfAction.Value)
            {
                case "AddFile":
                    if (FileUtils.FileExists(Server.MapPath(hfCurentPath.Value + txtFileName.Text)))
                    {
                        MessageUtils.ShowPre("文件已经存在。");
                    }

                    FileUtils.SaveFile(txtFileContent.Text, Server.MapPath(hfCurentPath.Value + txtFileName.Text));
                    MessageUtils.ShowRedirect("添加完成。","Template.aspx?path="+hfCurentPath.Value);

                    break;
                case "AddFolder":
                    if (FileUtils.FolderExists(Server.MapPath(hfCurentPath.Value + txtFileName.Text)))
                    {
                        MessageUtils.ShowPre("文件夹已经存在。");
                    }

                    FileUtils.CreateFolder( Server.MapPath(hfCurentPath.Value + txtFileName.Text));
                    MessageUtils.ShowRedirect("添加完成。","Template.aspx?path="+hfCurentPath.Value);
                    break;
                case "EditFolder":
                    if (txtFileName.Text != hfCurentFile.Value)
                    {
                        if (FileUtils.FolderExists(Server.MapPath(hfCurentPath.Value + txtFileName.Text)))
                        {
                            MessageUtils.ShowPre("文件夹已经存在。");
                        }

                        FileUtils.RenameFolder(Server.MapPath(hfCurentPath.Value + hfCurentFile.Value), Server.MapPath(hfCurentPath.Value + txtFileName.Text));
                        MessageUtils.ShowRedirect("重命名完成。", "Template.aspx?path=" + hfCurentPath.Value);
                    }
                    break;
                case "EditFile":
                    if (txtFileName.Text != hfCurentFile.Value)
                    {
                        if (FileUtils.FileExists(Server.MapPath(hfCurentPath.Value + txtFileName.Text)))
                        {
                            MessageUtils.ShowPre("文件已经存在。");
                        }
                    }

                    FileUtils.RenameFile(Server.MapPath(hfCurentPath.Value +hfCurentFile.Value), Server.MapPath(hfCurentPath.Value + txtFileName.Text));
                    FileUtils.SaveFile(txtFileContent.Text, Server.MapPath(hfCurentPath.Value + txtFileName.Text));
                    MessageUtils.ShowRedirect("修改完成。","Template.aspx?path="+hfCurentPath.Value);
                    break;
                default:
                    break;
            }
        }
    }
}